# -*- coding: utf-8 -*-
"""
@Time ： 2023/4/20 21:06
@Auth ： 叶东宇
@File ：reverseString2k.py
"""

def reverseString2k(lis,k):
    left = 0
    right = k-1
    tmp_k = 2*k-1
    while tmp_k < len(lis)-1:
        # 开始交换前k个
        tmp_r = right
        tmp_l = left
        while left < right:
            tmp = lis[left]
            lis[left] = lis[right]
            lis[right] = tmp
            left += 1
            right -= 1
        # 交换完毕 需要将left和right的位置重置
        right = tmp_r
        left = tmp_l
        left += 2*k
        right += 2*k
        tmp_k += 2*k
    if right > len(lis)-1:
        right = len(lis)-1
        while left < right:
            tmp = lis[left]
            lis[left] = lis[right]
            lis[right] = tmp
            left += 1
            right -= 1
        return lis
    else:
        while left < right:
            tmp = lis[left]
            lis[left] = lis[right]
            lis[right] = tmp
            left += 1
            right -= 1
        return lis


def from_leetCode(lis, k):

    lis = ''.join(lis)
    p = 0
    while p < len(lis):
        lis = lis[:p] + lis[p:p+k][::-1] + lis[p+k:]
        p += 2*k
    return lis

if __name__ == "__main__":
    lis = ['1','2','3','4','5','6','7']
    k = 2
    print(lis)
    # print(reverseString2k(lis,k))
    print(from_leetCode(lis, k))